package com.chinasofti.zbest.web;


import com.chinasofti.zbest.dao.model.User;
import com.chinasofti.zbest.dao.model.UserAddr;
import com.chinasofti.zbest.service.IUserAddrService;
import com.chinasofti.zbest.service.info.UserAddrInfo;
import com.chinasofti.zbest.web.form.AddressFrom;
import com.chinasofti.zbest.web.form.UserAddrForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.chinasofti.zbest.web.BaseController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author zbest
 * @since 2019-05-28
 */
@Controller
@RequestMapping("/address")
@Slf4j
public class UserAddrController extends BaseController {

    @Autowired
    private IUserAddrService iUserAddrService;


    /**
     * 跳转 添加 地址页面
     *
     * @param session
     * @param modelMap
     * @return
     */
    @RequestMapping("increaseAddr")
    public String increaseAddr(HttpSession session, ModelMap modelMap) {

        System.out.println("跳转增加地址页面");
        User user = (User) session.getAttribute("user");
        List<UserAddr> userAddrList = iUserAddrService.findByIdAddr(user.getUserId());
        log.info("useraddrList{}"+userAddrList);

        if (userAddrList.isEmpty()){
                modelMap.addAttribute("msg","你的地址栏空空如也，<a href='address/addAddress'>请添加数据</a>");
                return "user/address";
            }
        /*User user = (User) session.getAttribute("user");
       List<UserAddFrom> userAddrs = iUserAddrService.queryAllById(user.getUserId());

        modelMap.addAttribute("useraddrs", user);*/
        modelMap.addAttribute("useraddrs",userAddrList );
        return "user/address";
    }

    /**
     * 添加地址方法
     *
     * @param session
     * @param modelMap
     * @return
     */
    @RequestMapping("addAddress")
    public String addAddress(HttpSession session, AddressFrom address, ModelMap modelMap, UserAddrForm userAddr) {
        log.info("添加地址的方法");
        User user = (User) session.getAttribute("user");

        userAddr.setAddress(address.getCity() + " " + address.getCountry() + " " + address.getDetail());
        userAddr.setIsDefault(0);
        userAddr.setModifiedTime(LocalDateTime.now());
        userAddr.setUserId(user.getUserId());
        userAddr.setZip(address.getZip());
        userAddr.setUserAddrId(1);
        userAddr.setReceivingName(address.getReceivingName());
        userAddr.setReceivingPhone(address.getReceivingPhone());
        log.info("{}" + userAddr);


//        UserAddr userAddr2 = new UserAddr();

        UserAddrInfo userAddrInfo = new UserAddrInfo();
        BeanUtils.copyProperties(userAddr, userAddrInfo);

        log.info(userAddr.toString());

        iUserAddrService.add(userAddrInfo);


        return "user/address";
    }


}

